package com.lb.pdm;

import java.io.*;
import java.nio.file.*;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Fix Entity class annotations tool - Phase 2
 * Fix remaining import statements
 */
public class FixEntityAnnotations {
    
    private static final String ENTITY_PATH = "src/main/java/com/lb/pdm/entity";
    
    public static void main(String[] args) {
        try {
            Path entityDir = Paths.get(ENTITY_PATH);
            if (!Files.exists(entityDir)) {
                System.err.println("Entity directory not exists: " + entityDir.toAbsolutePath());
                return;
            }
            
            List<Path> javaFiles = Files.walk(entityDir)
                    .filter(path -> path.toString().endsWith(".java"))
                    .collect(Collectors.toList());
            
            System.out.println("Found " + javaFiles.size() + " Java files");
            
            for (Path javaFile : javaFiles) {
                fixImports(javaFile);
                System.out.println("Fixed imports: " + javaFile.getFileName());
            }
            
            System.out.println("\n=== Import fix completed! ===");
            
        } catch (Exception e) {
            System.err.println("Error during fix process: " + e.getMessage());
            e.printStackTrace();
        }
    }
    
    private static void fixImports(Path javaFile) throws IOException {
        String content = Files.readString(javaFile);
        String originalContent = content;
        
        // Remove old swagger imports
        content = content.replaceAll("import io\\.swagger\\.annotations\\.ApiModel;\\s*\\n", "");
        content = content.replaceAll("import io\\.swagger\\.annotations\\.ApiModelProperty;\\s*\\n", "");
        
        // Remove old lombok imports
        content = content.replaceAll("import lombok\\.Getter;\\s*\\n", "");
        content = content.replaceAll("import lombok\\.Setter;\\s*\\n", "");
        
        // Add new imports if needed
        if (content.contains("@Schema") && !content.contains("import io.swagger.v3.oas.annotations.media.Schema;")) {
            // Find the position after the last import statement
            String[] lines = content.split("\\n");
            StringBuilder newContent = new StringBuilder();
            boolean importAdded = false;
            
            for (String line : lines) {
                newContent.append(line).append("\n");
                
                // Add Schema import after lombok.Data import
                if (line.contains("import lombok.Data;") && !importAdded) {
                    newContent.append("import io.swagger.v3.oas.annotations.media.Schema;\n");
                    importAdded = true;
                }
            }
            
            if (importAdded) {
                content = newContent.toString();
            }
        }
        
        // Add @Data import if needed and not exists
        if (content.contains("@Data") && !content.contains("import lombok.Data;")) {
            // Find the position after the last import statement
            String[] lines = content.split("\\n");
            StringBuilder newContent = new StringBuilder();
            boolean importAdded = false;
            
            for (String line : lines) {
                newContent.append(line).append("\n");
                
                // Add Data import after other imports
                if (line.startsWith("import java.") && !importAdded) {
                    // Look ahead to see if there are more java imports
                    boolean hasMoreJavaImports = false;
                    for (int i = 0; i < lines.length; i++) {
                        if (lines[i].equals(line)) {
                            for (int j = i + 1; j < lines.length; j++) {
                                if (lines[j].startsWith("import java.")) {
                                    hasMoreJavaImports = true;
                                    break;
                                } else if (lines[j].startsWith("import ")) {
                                    break;
                                }
                            }
                            break;
                        }
                    }
                    
                    if (!hasMoreJavaImports) {
                        newContent.append("import lombok.Data;\n");
                        importAdded = true;
                    }
                }
            }
            
            if (importAdded) {
                content = newContent.toString();
            }
        }
        
        // Only write to file when content changed
        if (!content.equals(originalContent)) {
            Files.writeString(javaFile, content);
        }
    }
}